package com.spring.gateway.snail;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Description TODO
 * @Author xueguoping
 * @Date 2021/5/12 9:21
 */
@SpringBootApplication
public class SnailGateWayApplication {

	public static void main(String[] args) {
		SpringApplication.run(SnailGateWayApplication.class,args);
	}

	/**
	 * 创建用户唯一标识，使用IP作为用户唯一标识来进行限流
	 * @return
	 */
	@Bean("ipKeyResolver")
	public KeyResolver userKeyResolver(){
		return new KeyResolver() {
			@Override
			public Mono<String> resolve(ServerWebExchange exchange) {
				// 获取用户IP
				String hostString = exchange.getRequest().getRemoteAddress().getHostString();
				System.out.println(hostString);
				return Mono.just(hostString);
			}
		};
	}
}
